package com.idanchuang.database.manage.service.impl;

import com.alibaba.druid.pool.DruidDataSource;
import com.idanchuang.database.manage.config.DynamicDataSource;
import com.idanchuang.database.manage.model.Connection;
import com.idanchuang.database.manage.service.ConnectService;
import com.mysql.cj.jdbc.Driver;
import org.springframework.stereotype.Service;

import java.sql.SQLException;

/**
 * @param
 * @author:zhangmengsi
 * @description:
 * @date:Created in 11:42 2021/2/5
 */
@Service
public class ConnectServiceImpl implements ConnectService {
    @Override
    public boolean connectDB(Connection connection) {
        DruidDataSource druidDataSource = new DruidDataSource();
        druidDataSource.setUrl("jdbc:mysql://"+connection.getUrl()+":"+connection.getPort()+"/?useUnicode=true&characterEncoding=utf-8&useSSL=true&serverTimezone=UTC");
        druidDataSource.setUsername(connection.getUsername());
        druidDataSource.setPassword(connection.getPassword());
        DynamicDataSource.dataSourcesMap.put(connection.getUrl(), druidDataSource);
        DynamicDataSource.setDataSource(connection.getUrl());
        return true;
    }
}
